import { required, minLength, between } from 'vuelidate/lib/validators';

export function telNumPattern(val) {
    return /^1[34578]\d{9}$/.test(val)
};

export function emailPattern(val) {
    return /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(val);
}

export function IDcardPattern(val) {
    let reg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
    let str = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
    let validNum = [1, 0, 'x', 9, 8, 7, 6, 5, 4, 3, 2];
    let sum = 0,
        remainder, validUnit, arr, last;
    if (reg.test(val)) {
        arr = val.split('');
        last = arr.pop();
        arr.forEach((item, index) => {
            sum += ((+item) * (+str[index]));
        });
        remainder = sum % 11;
        validUnit = validNum[remainder];
        return (validUnit + '') == ('' + last).toLowerCase();
    } else {
        return false;
    }
    return;
}

export function ChinesePattern(val) {
    let reg = /[\u4E00-\u9FA5]/;
    return reg.test(val);
}

export function nowDatePattern(val) {
    let date = new Date(val).getTime();
    let nowDate = Date.now();
    return (date - nowDate > 3600 * 1000 * 24);
}

export function nowDatePattern2(val) {
    let date = new Date(val).getTime();
    let nowDate = Date.now();
    return (date - nowDate > 3600 * 1000 * 2);
}

export function oldDatePattern(val) {
    let date = new Date(val).getTime();
    let nowDate = Date.now();
    return (date - nowDate < 3600000 * 24);
}

export function BirthPattern(...arg) {
    let [start, end] = arg;
    let startYearTime = start ? start * 3600 * 1000 * 24 * 365 : null;
    let endYearTime = end ? end * 3600 * 1000 * 24 * 365 : null;
    return function(val) {
        let date = new Date(val).getTime();
        let nowDate = Date.now();
        let intev = nowDate - date;
        if (arg.length > 1) {
            return intev >= startYearTime && intev <= endYearTime
        }
        return (nowDate - date >= startYearTime);
    }
}

export function carNoPatten(val) {
    let reg = /^[\u4e00-\u9fa5][A-Z](?![A-HJ-NP-Z]{5})[A-HJ-NP-Z\d]{5}$/;
    return reg.test(val);
}

export function floatPatten(...args) {
    let [start, end] = args;
    let pattern = end ? `{${start},${end}}` : (start ? `{1,${start}}` : '+');
    let str = `^[\\d]+(\.([\\d]${pattern}))?$`;
    return function(val) {
        let reg = new RegExp(str, 'i');
        return reg.test(val);
    }
}
